Test Series - Data Structure

Test Number 44/115

Q: What will be the minimum number of jumps required to reach the end of the array arr[] = {1,3,6,3,6,8,5}?
A. 1
B. 2
C. 3
D. not possible to reach the end
Solution: Each element of the array represents the maximum number of steps that can be taken forward from that element. If the first element is 0 then it is not possible to reach the end.
Q: What will be the minimum number of jumps required to reach the end of the array arr[] ={0,1,3,6,3,6,8,5}?
A. 1
B. 2
C. 3
D. not possible to reach the end
Solution: Each element of the array represents the maximum number of steps that can be taken forward from that element. So as the first element here is 0 so we cannot move any further from the first element. Thus, it is not possible to reach the end of the array.
Q: What will be the output of the following code?

#include  
using namespace std; 
 
int func(int arr[], int s, int e) 
{
   if (s == e) 
	return 0; 
   if (arr[s] == 0) 
	return INT_MAX; 
 
int min = INT_MAX; 
for (int i = s + 1; i <= e && i <= s + arr[s]; i++) 
{ 
	int jumps = func(arr, i, e); 
	if(jumps != INT_MAX && jumps + 1 < min) 
		min = jumps + 1; 
} 
return min; 
}
 
int main() 
{ 
	int arr[] = {1, 3, 6, 3, 8, 5}; 
	int n = sizeof(arr)/sizeof(arr[0]); 
	cout << func(arr, 0, n-1); 
	return 0; 
}
A. 1
B. 2
C. 3
D. error
Solution: The given code finds the minimum number of steps required to reach the end of the array by using recursion. So the output will be 3.
Q: What will be the output of the following code?

#include  
using namespace std; 
 
int min(int x, int y) 
{ return (x < y)? x: y; } 
 
int func(int arr[], int n) 
{ 
 
	int *jump = new int[n]; 
	int i, j; 
 
	if (n == 0 || arr[0] == 0) 
		return INT_MAX; 
 
	jump[0] = 0; 
 
	for (i = 1; i < n; i++) 
	{ 
		jump[i] = INT_MAX; 
		for (j = 0; j < i; j++) 
		{ 
			if (i <= j + arr[j] && jumps[j] != INT_MAX) 
			{ 
				jump[i] = min(jump[i], jump[j] + 1); 
				break; 
			} 
		} 
	} 
	return jump[n-1]; 
} 
 
int main() 
{ 
	int arr[] = {1, 3, 6, 1, 9,7}; 
	int size = sizeof(arr)/sizeof(int); 
	cout<< func(arr,size); 
	return 0; 
}
A. 1
B. 2
C. 3
D. error
Solution: The given code finds the minimum number of steps required to reach the end of the array by using dynamic programming. So the output will be 3.
Q: What will be the time complexity of the following code?

#include  
using namespace std; 
 
int min(int x, int y) 
{ return (x < y)? x: y; } 
 
int func(int arr[], int n) 
{ 
 
	int *jump = new int[n]; 
	int i, j; 
 
	if (n == 0 || arr[0] == 0) 
		return INT_MAX; 
 
	jump[0] = 0; 
 
	for (i = 1; i < n; i++) 
	{ 
		jump[i] = INT_MAX; 
		for (j = 0; j < i; j++) 
		{ 
			if (i <= j + arr[j] && jumps[j] != INT_MAX) 
			{ 
				jump[i] = min(jump[i], jump[j] + 1); 
				break; 
			} 
		} 
	} 
	return jump[n-1]; 
} 
 
int main() 
{ 
	int arr[] = {1, 3, 6, 1, 9,7}; 
	int size = sizeof(arr)/sizeof(int); 
	cout<< func(arr,size); 
	return 0; 
}
A. O(n log n)
B. O(n)
C. O(n1/2)
D. O(n2)
Solution: The given code finds the minimum number of steps required to reach the end of an array by using dynamic programming. As there is a nested loop in the code so the time complexity will be O(n2).
Q: What will be the minimum number of jumps required to reach the end of the array arr[] = {1,2,0,0,3,6,8,5}?
A. 1
B. 2
C. 3
D. not possible to reach the end
Solution: Each element of the array represents the maximum number of steps that can be taken forward from that element. So we cannot move any further after reaching the second element hence it is impossible to reach the end of the array.
Q: It is not possible to find the minimum number of steps to reach the end of an array in linear time.
A. true
B. false
C. ....
D. ....
Solution: It is possible to find the minimum number of steps to reach the end of an array in O(n) time complexity. So it is the fastest possible method of finding the minimum number of steps to reach the end of an array.
Q:  In how many different ways we can reach the end of the array arr[]={1,3,5,8,9}?
A. 1
B. 2
C. 3
D. 4
Solution: There are 4 possible ways in which we can reach the end of the array. The possible paths are – 1->3->5->8->9, 1->3->5->9, 1->3->8->9, 1->3->9.
Q: What will be the output of the following code?

#include  
using namespace std; 
 
void func(int arr[], int n) 
{  
	int count[n]; 
	memset(count, 0, sizeof(count)); 
 
	for (int i=n-2; i>=0; i--) 
	{ 
		if (arr[i] >= n - i - 1) 
			count[i]++; 
 
		for (int j=i+1; j < n-1 && j <= arr[i] + i; j++) 
 
			if (count[j] != -1) 
				count[i] += count[j]; 
 
		if (count[i] == 0) 
			count[i] = -1; 
	} 
 
	for (int i=0; i
A. 3
B. 4
C. 4 4 2 1 0
D. 4 2 2 0 1
Solution: The given code finds the number of possible ways to reach the end of an array from each element. So the output will be 4 4 2 1 0.
Q: What will be the worst case time complexity of the following code?

#include  
using namespace std; 
 
void func(int arr[], int n) 
{  	
	int count[n]; 
	memset(count, 0, sizeof(count)); 
 
	for (int i=n-2; i>=0; i--) 
	{ 
		if (arr[i] >= n - i - 1) 
			count[i]++; 
 
		for (int j=i+1; j < n-1 && j <= arr[i] + i; j++) 
 
			if (count[j] != -1) 
				count[i] += count[j]; 
 
		if (count[i] == 0) 
			count[i] = -1; 
	} 
 
	for (int i=0; i
A. O(n1/2)
B. O(n)
C. O(n3/2)
D. O(n2)
Solution: The given code finds the number of possible ways to reach the end of an array from each element. By observing the nested loop in the code we can say that the worst case time complexity will be O(n2).

You Have Score    /10